package stuServlet;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.jspsmart.upload.SmartUpload;
import com.jspsmart.upload.SmartUploadException;

@SuppressWarnings("serial")
public class DoSmartUploadServlet extends HttpServlet {
	
	private String path;

	public DoSmartUploadServlet() {
		super();
	}

	public void destroy() {
		super.destroy(); // Just puts "destroy" string in log
		// Put your code here
	}
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		this.execute(request, response);
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		this.execute(request, response);

	}
	public void execute(HttpServletRequest request, HttpServletResponse response)
	throws ServletException, IOException {
		SmartUpload su=new SmartUpload();
		su.initialize(getServletConfig(), request, response);
		try {
			su.upload();
			su.save(path);
			response.sendRedirect("stuJsp/upload.jsp");
		} catch (SmartUploadException e) {
			e.printStackTrace();
		}
		
	}

	public void init() throws ServletException {
		path=getServletConfig().getInitParameter("path");
	}

}
